/*!
 * \file PTXToSASSTranslator.cpp
 */

#include <fstream>

#include <hydrazine/interface/Version.h>
#include <hydrazine/interface/ArgumentParser.h>
#include <hydrazine/interface/debug.h>

#include <ocelot/ir/interface/Module.h>
#include <ocelot/ir/interface/PTXKernel.h>
#include <ocelot/ir/interface/SASSKernel.h>
#include <ocelot/translator/interface/PTXToSASSTranslator.h>
#include <ocelot/tools/PTXToSASSTranslator.h>


void PTXToSASSTranslator::translate() {
	if (input.empty()) {
		assertM(false, "input filename is empty");
		return;
	}
	if (output.empty()) {
		assertM(false, "output filename is empty");
		return;
	}

	translator::PTXToSASSTranslator translator;
	ir::Module module(input);
	std::ofstream sassout(output);
	hydrazine::Version ver;

	sassout << "/* generated by gpuocelot " << ver.toString()
		<< " */" << std::endl;
	for(ir::Module::KernelMap::const_iterator it =
		module.kernels().begin();
		it != module.kernels().end(); it++) {
		ir::PTXKernel* ptx = dynamic_cast<ir::PTXKernel*>(it->second);
		ir::SASSKernel* sass = dynamic_cast<ir::SASSKernel*>(translator.translate(ptx));
		sass->assemble();
		sassout << std::endl;
		sassout << sass->code();
	}
	sassout.close();
}


int main(int argc, char* argv[])
{
	hydrazine::ArgumentParser parser(argc, argv);
	PTXToSASSTranslator trans;

	parser.description("The Translator from PTX to SASS.");
	parser.parse( "-i", "--input", trans.input, "", "input PTX file.");
	parser.parse( "-o", "--output", trans.output, "", "output SASS file.");
	parser.parse();

	trans.translate();

	return 0;
}
